package com.hava.cloud_study.service;

import java.io.InputStream;

import com.hava.cloud_study.entity.User;
import com.hava.cloud_study.repository.UserOp;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class HelloMyBatis {
    public static void main(String[] args) {
        // 1. 声明配置⽂件的⺫录渎职
        String resource = "mybatis-conf.xml";
        // 2. 加载应⽤配置⽂件
        InputStream is = HelloMyBatis.class.getClassLoader()
                .getResourceAsStream(resource);
        // 3. 创建SqlSessonFactory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(is);
        // 4. 获取Session
        SqlSession session = sessionFactory.openSession();
        try {
            // 5. 获取操作类
            UserOp userOp = session.getMapper(UserOp.class);
            // 6. 完成查询操作
            User user = userOp.getUser(25);
            System.out.println(user.getId() + " " + user.getUserName() + " ");
                    System.out.println(user.getCourses().get(0).getCourseName() +" ");
            System.out.println(user.getCourses().get(0).getTeacher().getTeacherName());
        } finally {
            // 7.关闭Session
            session.close();
        }
    }
}